package com.qsmx.qingqiuticket.app.delegate.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qsmx.core.util.StringUtil;
import com.qsmx.qingqiuticket.app.R;
import com.qsmx.qingqiuticket.app.delegate.holder.SessionListHolder;
import com.qsmx.qingqiuticket.app.entity.SessionListEntity;

import java.util.List;

public class SessionListAdapter extends RecyclerView.Adapter<SessionListHolder> {

    private Context mContext;

    private List<SessionListEntity.BodyBean> mData;
    private int ticketNum;
    private boolean isShowDesc = false;

    //定义接口
    public static interface OnItemClickListener {
        void onClick(View view, int position);

    }

    public List<SessionListEntity.BodyBean> getData() {
        return mData;
    }

    public void setData(List<SessionListEntity.BodyBean> mData, int ticketNum, boolean isShowDesc) {
        this.mData = mData;
        this.ticketNum = ticketNum;
        this.isShowDesc = isShowDesc;
    }

    //声明自定义的监听接口
    private SessionListAdapter.OnItemClickListener monItemClickListener = null;

    //提供set方法
    public void setonItemClickListener(SessionListAdapter.OnItemClickListener listener) {
        this.monItemClickListener = listener;
    }

    public SessionListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public SessionListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SessionListHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_session_list, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull SessionListHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvSession.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monItemClickListener.onClick(v, position);
            }
        });
        holder.tvSessionTime.setText(mData.get(position).getSessionStartTime() + "-" + mData.get(position).getSessionEndTime());
        if ((mData.get(position).getOfflineSeat() <= mData.get(position).getOfflineOrder())) {
            holder.tvSessionTime.setTextColor(mContext.getColor(R.color.gary_text));
            holder.tvSessionStatus.setText("已售罄");
            holder.tvSessionStatus.setTextColor(mContext.getColor(R.color.gary_text));
            holder.tvSession.setClickable(false);
            holder.tvSession.setBackgroundResource(R.drawable.round_corner_dark_gray_bg_gray_line);
            holder.clContent.setBackgroundResource(R.drawable.round_corner_white_bg_gray_line);
        } else {
            if ((mData.get(position).getOfflineSeat() - mData.get(position).getOfflineOrder() < ticketNum)) {
                holder.tvSessionTime.setTextColor(mContext.getColor(R.color.gary_text));
                holder.tvSessionStatus.setText("已售罄");
                holder.tvSessionStatus.setTextColor(mContext.getColor(R.color.gary_text));
                holder.tvSession.setClickable(false);
                holder.tvSession.setBackgroundResource(R.drawable.round_corner_dark_gray_bg_gray_line);
                holder.clContent.setBackgroundResource(R.drawable.round_corner_white_bg_gray_line);
            } else {
                holder.tvSessionTime.setTextColor(mContext.getColor(R.color.black));
                holder.tvSessionStatus.setText("可售");
                holder.tvSessionStatus.setTextColor(mContext.getColor(R.color.back_blue));
                holder.tvSession.setClickable(true);
                holder.tvSession.setBackgroundResource(R.drawable.round_corner_back_btn_bg);
                holder.clContent.setBackgroundResource(R.drawable.round_corner_white_bg_blue_line);
            }
        }

        if (isShowDesc) {
            if (StringUtil.isNotEmpty(mData.get(position).getSessionDesc())) {
                holder.tvSessionDesc.setVisibility(View.VISIBLE);
                holder.tvSessionDesc.setText(mData.get(position).getSessionDesc());
            } else {
                holder.tvSessionDesc.setVisibility(View.GONE);
            }
        } else {
            holder.tvSessionDesc.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        if (mData == null) {
            return 0;
        } else {
            return mData.size();
        }
    }
}
